package Datebase;

import Datebase.*;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.text.StyledDocument;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import 	redis.clients.jedis.Jedis;

public class slogin extends JFrame {

    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new slogin();

    }
    public slogin() {

        JFrame student = new JFrame("学生登录系统");
        //图标
        URL resource = slogin.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        student.setIconImage(image);
        //窗口尺寸
        student.setSize(400,200);
        student.setLocationRelativeTo(null);

        student.setLayout(null);
        //

        JLabel label1 = new JLabel("学   号：");
        label1.setFont(new Font("黑体",Font.PLAIN,15));
        label1.setBounds(100,-20,100,100);
        student.add(label1);

        JTextField textField1 = new JTextField();
        textField1.setFont(new Font("微软雅黑",Font.PLAIN,15));
        textField1.setBounds(170,20,110,20);
        textField1.setColumns(10);
        student.add(textField1);



        JLabel label2 = new JLabel("密   码：");
        label2.setFont(new Font("黑体",Font.PLAIN,15));
        label2.setBounds(100,10,100,100);
        student.add(label2);

        JPasswordField textField2 = new JPasswordField();
        textField2.setFont(new Font("微软雅黑",Font.PLAIN,15));
        textField2.setBounds(170,50,110,20);
        textField2.setColumns(10);
        student.add(textField2);

        JButton button3 = new JButton("登录");
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == button3) {

                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                    Connection connection = null;
                    try {
                        connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE" ,"HR","root");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }

                    try {
                        String uid = textField1.getText();// 从文本框获取学号
                        Jedis jedis = new Jedis("127.0.0.1",6379);// 建立redis连接
                        String password = jedis.get(uid);    // 从redis里面通过输入的学号对应的键值找到密码

                        if(password == textField2.getText()) {   //判断里面的password是不是空如果空的话，执行else 如果输入的是数据库里正确的号，同时就会将它存到redis里

                            new smenu(textField1.getText());

                        }
                        else{
                            String pw = "";
                            String un = "";
                            Statement statement = connection.createStatement();
                            ResultSet resultSet = statement.executeQuery("select SNO,SCODE From STUDENT where SNO='" + textField1.getText() + "'");

                            while (resultSet.next()) {
                                un = resultSet.getString(1);
                                pw = resultSet.getString(2);
                            }


                        if (!textField1.getText().equals(un) || textField1.getText().equals("")) {
                            JOptionPane.showMessageDialog(null, "无此用户！",
                                    "提示", JOptionPane.WARNING_MESSAGE);
                        } else if ( !textField2.getText().equals(pw)) {
                            JOptionPane.showMessageDialog(null, "密码错误，请重新输入！",
                                    "提示", JOptionPane.WARNING_MESSAGE);
                        } else {
                            jedis.set(textField1.getText(),pw);
                            new smenu(textField1.getText());
                        }


                        connection.close();
                        }

                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }

                }
            }
        });
        button3.setFont(new Font("黑体",Font.PLAIN,13));
        button3.setBounds(100,80,80,30);
        student.add(button3);


        JButton button4 = new JButton("返回");
        button4.setFont(new Font("黑体",Font.PLAIN,13));
        button4.setBounds(200,80,80,30);
        student.add(button4);

            //返回上一层
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new datab();
            }
        });
        student.setVisible(true);
    }

}

